"use strict";
/* Controllers */
app.controller("userAddController", ['$scope', '$location', '$rootScope', '$routeParams', 'userAddService', function ($scope, $location, $rootScope, $routeParams, userAddService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.id = $routeParams.id; // 如果ID!=0为更新
    vm.d = {};
    vm.d.isTestDefine = [{id:0, name:'0-正式'},{id:1, name:'1-测试'}];
    var selectIsTestDefine = function(id) {
        for(var i=0;i<vm.d.isTestDefine.length; ++i) {
            var item = vm.d.isTestDefine[i];
            if(item.id==id) {
                return item;
            }
        }
        return vm.d.isTestDefine[0];
    };

    var errorFn = function (result) {
        alert(result);
    };
    $scope.addUser = function () {
        var vm = $scope.vm;
        if(vm.p.password2!=vm.p.password) {
            alert("两次输入密码不一致");
            return;
        }
        var users = {
            id: vm.p.id,
            userName: vm.p.userName,
            userCn: vm.p.userCn,
            email: vm.p.email,
            mobile: vm.p.mobile,
            isTest: vm.p.isTestDefine.id,
            leader: vm.p.leader,
            password:vm.p.password,
            dynamicCode: vm.p.dynamicCode
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                //window.location = "#/income/user_list/0";
                alert("执行成功！")
            } else {
                alert(response.message);
            }
        };
        userAddService.addUser(users, procFn, errorFn);
    };



    var countdown=60;
    $scope.sendDynamicCode = function() {
        var validateMobile = function (e) {
            var mobilePattern = /^1[23456789]\d{9}$/g;
            return mobilePattern.test(e)
        };
        if(!validateMobile(vm.p.mobile)) {
            alert("手机号码无效.");
            return;
        }

        var obj = $("#btn");
        var setTime = function (obj) { //发送验证码倒计时
            if (countdown == 0) {
                obj.attr('disabled',false);
                obj.val("发送动态密码");
                countdown = 60;
                return;
            } else {
                obj.attr('disabled',true);
                obj.val("重新发送(" + countdown + ")");
                countdown--;
            }
            setTimeout(function(){setTime(obj)} ,1000);
        };
        setTime(obj);

        var params = {
            phoneNumber: vm.p.mobile
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("短信已发送！")
            } else {
                alert(response.message);
            }
        };
        userAddService.sendDynamicCode(params, procFn, errorFn);
    };
    $scope.initializeController = function() {
        vm.p.isTestDefine = selectIsTestDefine(0);
    };
    $scope.initializeController();
}]);


/* Services */
app.service('userAddService', ['ajaxService', function (ajaxService) {
    this.sendDynamicCode = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "./sendDynamicCode2", successFunction, errorFunction);
    };


    this.addUser = function (users, successFunction, errorFunction) {
        ajaxService.AjaxPost(users, "./addUser.do", successFunction, errorFunction);
    };
}]);

